<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 600px;
            margin: 50px auto;
            padding: 20px;
            background: #f5f5f5;
        }
        .test-box {
            background: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        input {
            width: 100%;
            padding: 10px;
            margin: 10px 0;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        button {
            width: 100%;
            padding: 12px;
            background: #667eea;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }
        button:hover {
            background: #5568d3;
        }
        #result {
            margin-top: 20px;
            padding: 15px;
            border-radius: 4px;
            white-space: pre-wrap;
            font-family: monospace;
            font-size: 12px;
        }
        .success { background: #d4edda; color: #155724; }
        .error { background: #f8d7da; color: #721c24; }
    </style>
</head>
<body>
    <div class="test-box">
        <h2>JWT 登录测试</h2>
        <input type="text" id="username" placeholder="用户名" value="zgq">
        <input type="password" id="password" placeholder="密码" value="">
        <button onclick="testLogin()">测试登录</button>
        <div id="result"></div>
    </div>

    <script>
        async function testLogin() {
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            const resultDiv = document.getElementById('result');
            
            resultDiv.textContent = '正在发送请求...';
            resultDiv.className = '';
            
            try {
                const response = await fetch('http://127.0.0.1:8000/creation/auth/login', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({ username, password })
                });
                
                const data = await response.json();
                
                if (response.ok && data.code === 200) {
                    resultDiv.className = 'success';
                    resultDiv.textContent = '✅ 登录成功！\n\n' + JSON.stringify(data, null, 2);
                    
                    // 保存 Token
                    if (data.data.token) {
                        localStorage.setItem('token', data.data.token);
                        console.log('Token 已保存:', data.data.token);
                    }
                } else {
                    resultDiv.className = 'error';
                    resultDiv.textContent = '❌ 登录失败\n\n' + JSON.stringify(data, null, 2);
                }
            } catch (error) {
                resultDiv.className = 'error';
                resultDiv.textContent = '❌ 网络错误\n\n' + error.message + '\n\n可能原因：\n1. 后端服务未启动\n2. CORS 配置问题\n3. 网络连接问题';
            }
        }
    </script>
</body>
</html>
